package com.imooc.alllearning.common;

import com.imooc.alllearning.exception.ErrorCodeEnum;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * Created by lijian on 2020/3/27 11:27
 */
@Data
@ApiModel(
        value = "统一返回结果实体",
        description = "封装统一返回结果信息实体"
)
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 5730089387512049795L;
    @ApiModelProperty(
            name = "success",
            value = "是否成功",
            required = true,
            dataType = "Boolean"
    )
    private Boolean success;//是否成功
    @ApiModelProperty(
            name = "code",
            value = "编码",
            required = false,
            dataType = "String"
    )
    private String code;//编码
    @ApiModelProperty(
            value = "参数信息"
    )
    private String message;//参数信息
    @ApiModelProperty(
            value = "泛型结果T"
    )
    private T result;//结果

    /**
     * 成功
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success(T result){
        ResponseResult<T> responseResult = new ResponseResult<T>();
        responseResult.setSuccess(true);
        responseResult.setResult(result);
        return responseResult;
    }

    /**
     * 失败
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> failure(String code, String message){
        ResponseResult<T> responseResult = new ResponseResult<T>();
        responseResult.setSuccess(false);
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T> ResponseResult<T> failure(ErrorCodeEnum errorCodeEnum){
        return failure(errorCodeEnum.getCode(), errorCodeEnum.getMessage());
    }
}
